package controllers;

import play.*;
import play.data.validation.Required;
import play.mvc.*;

import java.util.*;

import models.*;

public class ChooseMeal extends Controller {
	
	public static void choose(String uid) {
		Invite invite = Invite.find("byUUID", uid).first();
		Appointment appointment = invite.appointment;
		Person invitee = invite.invitee;
		List<Meal> meals = Meal.find("byRestaurant", appointment.restaurant).fetch();
		
		render(appointment, invitee, meals, invite);
	}
	
	public static void done(@Required Long invite_id, Long[] meal_list, int[] amount) {
		Invite invite = Invite.findById(invite_id);
		MealOrder order = invite.order;
		if (meal_list != null && amount != null){
			for (int i = 0; i < meal_list.length; i++) {
				Meal meal = Meal.findById(meal_list[i]);
				for (int j = 0; j < amount[i]; j++) {
					order.meals.add(meal);
				}
			}
		}
		order.save();
		render(order);
	}
}
